unit uBaseFormDB;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uBaseForm, Menus, ImgList, StdCtrls, FMTBcd, SqlExpr, ExtCtrls,
  ComCtrls, Buttons, ToolWin, DB, Grids, DBGrids, Mask, DBClient, SimpleDS,
  StdActns, ActnList;

type
  TfBaseFormDB = class(TfBaseForm)
    CoolBar: TCoolBar;
    paTools: TPanel;
    btIncluir: TSpeedButton;
    btEditar: TSpeedButton;
    btAtualizar: TSpeedButton;
    btExcluir: TSpeedButton;
    btGravar: TSpeedButton;
    btCancelar: TSpeedButton;
    btFerramentas: TSpeedButton;
    btAjuda: TSpeedButton;
    PageControl: TPageControl;
    tshBase: TTabSheet;
    StatusBar: TStatusBar;
    imgDBStatus: TImage;
    imgDBOrder: TImage;
    imgDBCounter: TImage;
    ckInsertSerie: TCheckBox;
    ActionList: TActionList;
    DataSetFirst: TAction;
    DataSetPrior: TAction;
    DataSetNext: TAction;
    DataSetLast: TAction;
    DataSetInsert: TAction;
    DataSetDelete: TAction;
    DataSetEdit: TAction;
    DataSetPost: TAction;
    DataSetCancel: TAction;
    DataSetRefresh: TAction;
    actClearField: TAction;
    actLocate: TAction;
    actHelp: TAction;
    actListRefresh: TAction;
    actForm: TAction;
    actLocateDataset: TAction;
    actCountRecord: TAction;
    actFilter: TAction;
    actOrder: TAction;
    actOrderDesc: TAction;
    actMultiSelect: TAction;
    actMultiDelete: TAction;
    actCopySelect: TAction;
    actExportRTF: TAction;
    actExportPDF: TAction;
    actExportExcel: TAction;
    actExportXML: TAction;
    actExportHTML: TAction;
    actExportTXT: TAction;
    actPrintConfig: TFilePrintSetup;
    actPrint: TAction;
    actPrintTextMode: TAction;
    ImageListAct: TImageList;
    PopupMenu: TPopupMenu;
    mniClearField: TMenuItem;
    mniSep1: TMenuItem;
    mniLocate: TMenuItem;
    mniFilter: TMenuItem;
    mniOrder: TMenuItem;
    mniOrderDec: TMenuItem;
    mniSep2: TMenuItem;
    mniListRefresh: TMenuItem;
    mniForm: TMenuItem;
    mniLocateDataset: TMenuItem;
    mniSep3: TMenuItem;
    mniCountRecord: TMenuItem;
    mniMultiSelect: TMenuItem;
    mniMultiDelete: TMenuItem;
    mniCopySelect: TMenuItem;
    Exportarseleo1: TMenuItem;
    mniExportRTF: TMenuItem;
    mniExportPDF: TMenuItem;
    mniExportExcel: TMenuItem;
    mniExportXML: TMenuItem;
    actExportHTML1: TMenuItem;
    XT1: TMenuItem;
    mniSep4: TMenuItem;
    mniPrintConfig: TMenuItem;
    mniPrint: TMenuItem;
    mniPrintTextMode: TMenuItem;
    ImageListTab: TImageList;
    dsSearchBase: TDataSource;
    tshSearch: TTabSheet;
    paBaseForm: TPanel;
    paTextSearch: TPanel;
    cbFields: TComboBox;
    lbField: TLabel;
    paSearch: TPanel;
    edTextSerarch: TMaskEdit;
    scdsSearchBase: TSimpleDataSet;
    procedure scdsSearchBaseAfterOpen(DataSet: TDataSet);
    procedure FormCreate(Sender: TObject);
    procedure actCountRecordExecute(Sender: TObject);
    procedure actClearFieldExecute(Sender: TObject);
    procedure btFerramentasClick(Sender: TObject);
    procedure DataSetFirstExecute(Sender: TObject);
    procedure DataSetPriorExecute(Sender: TObject);
    procedure DataSetNextExecute(Sender: TObject);
    procedure DataSetLastExecute(Sender: TObject);
    procedure DataSetInsertExecute(Sender: TObject);
    procedure DataSetDeleteExecute(Sender: TObject);
    procedure DataSetEditExecute(Sender: TObject);
    procedure DataSetPostExecute(Sender: TObject);
    procedure DataSetCancelExecute(Sender: TObject);
    procedure DataSetRefreshExecute(Sender: TObject);
  private
    { Private declarations }
    FFormDataSet: TDataSet;
    function GetFieldName(AControl: TComponent): string;
    function GetDataSource(AControl: TComponent): TDataSource;
    procedure SetFormDataSet(const Value: TDataSet);
  public
    { Public declarations }
    procedure BFBeforePost(DataSet: TDataSet);
  protected
    property FormDataSet: TDataSet read FFormDataSet write SetFormDataSet;
  end;

var
  fBaseFormDB: TfBaseFormDB;

implementation

uses DataSetExport, UFunctions, TypInfo, UDMDatabase;

{$R *.dfm}


function TfBaseFormDB.GetFieldName(AControl: TComponent): string;
begin
  Result := '';
  if AControl is TDBGrid then
    Result := (AControl as TDBGrid).SelectedField.FieldName
  else
    if IsPublishedProp(AControl, 'DataSource') then
      Result := GetPropValue(AControl, 'DataField');
end;

procedure TfBaseFormDB.scdsSearchBaseAfterOpen(DataSet: TDataSet);
var
  I: Integer;
begin
  inherited;
  for I := 0 to scdsSearchBase.Fields.Count - 1 do
    cbFields.Items.AddObject(scdsSearchBase.Fields[I].DisplayName, scdsSearchBase.Fields[0]);
end;

function TfBaseFormDB.GetDataSource(AControl: TComponent): TDataSource;
begin
  Result := nil;

  if IsPublishedProp(AControl, 'DataSource') then
    Result := TDataSource(GetObjectProp(AControl, 'DataSource'));;
end;

procedure TfBaseFormDB.actClearFieldExecute(Sender: TObject);
var
  AFieldName: string;
  ADataSource: TDataSource;
begin
  inherited;
  AFieldName  := GetFieldName(ActiveControl);
  ADataSource := GetDataSource(ActiveControl);

  if (AFieldName = '') or (not Assigned(ADataSource)) or (not Assigned(ADataSource.DataSet)) or ADataSource.DataSet.IsEmpty then
    Exit;

  if not( ADataSource.DataSet.State in [dsInsert, dsEdit] ) then
  begin
    ADataSource.DataSet.Edit;
    ADataSource.DataSet.FieldByName(AFieldName).Clear;
  end;

end;

procedure TfBaseFormDB.actCountRecordExecute(Sender: TObject);
var
  ADataSource: TDataSource;
begin
  inherited;
  ADataSource := GetDataSource(ActiveControl);
  if ( not Assigned(ADataSource) ) or ( not Assigned(ADataSource.DataSet) ) then
    Exit;
  StatusBar.Panels[5].Text := IntToStr( ADataSource.DataSet.RecordCount );
end;

procedure TfBaseFormDB.btFerramentasClick(Sender: TObject);
begin
  inherited;
  actCopySelect.Enabled  := actMultiSelect.Checked;
  actMultiDelete.Enabled := actMultiSelect.Checked;

  PopupMenu.Popup(Self.Left + btFerramentas.Left + 10, Self.Top + btFerramentas.Top + (btFerramentas.Height * 2));

end;

procedure TfBaseFormDB.FormCreate(Sender: TObject);
begin
  scdsSearchBase.Connection := dmDatabase.SQLConnection;
  PageControl.TabIndex := 0;

  inherited;

  FFormDataSet.BeforePost := BFBeforePost;

  if scdsSearchBase.DataSet.CommandText <> '' then
     scdsSearchBase.Open;

end;

procedure TfBaseFormDB.DataSetFirstExecute(Sender: TObject);
begin
  inherited;
  FormDataSet.First;
end;

procedure TfBaseFormDB.DataSetPriorExecute(Sender: TObject);
begin
  inherited;
  FormDataSet.Prior
end;

procedure TfBaseFormDB.DataSetNextExecute(Sender: TObject);
begin
  inherited;
  FormDataSet.Next;
end;

procedure TfBaseFormDB.DataSetLastExecute(Sender: TObject);
begin
  inherited;
  FormDataSet.Last;
end;

procedure TfBaseFormDB.DataSetInsertExecute(Sender: TObject);
begin
  inherited;
  FormDataSet.Append;
end;

procedure TfBaseFormDB.DataSetDeleteExecute(Sender: TObject);
begin
  inherited;
  
  if not FormDataSet.IsEmpty then
    FormDataSet.Delete;
end;

procedure TfBaseFormDB.DataSetEditExecute(Sender: TObject);
begin
  inherited;
  FormDataSet.Edit;
end;

procedure TfBaseFormDB.DataSetPostExecute(Sender: TObject);
begin
  inherited;
  FormDataSet.Post;
end;

procedure TfBaseFormDB.DataSetCancelExecute(Sender: TObject);
begin
  inherited;
  FormDataSet.Cancel;
end;

procedure TfBaseFormDB.DataSetRefreshExecute(Sender: TObject);
begin
  inherited;
  FormDataSet.Refresh;
end;

procedure TfBaseFormDB.SetFormDataSet(const Value: TDataSet);
begin
  FFormDataSet := Value;
  FFormDataSet.Open;
end;

procedure TfBaseFormDB.BFBeforePost(DataSet: TDataSet);
begin
  inherited;
  TClientDataSet(DataSet).ApplyUpdates(-1);
end;

end.
